#!/bin/bash

# init.d style script for bigdata HA services.  The script can be used
# to 'start' or 'stop' services.
#
# Environment:
#
# binDir   - The directory containing the installed scripts.
# pidFile  - The pid is written on this file.
#
# Misc.
# 
# See http://tldp.org/LDP/abs/html/index.html
#
# Note: Blank lines are significant in shell scripts.
#
# Note: Children must do "exit 0" to indicate success.
#
# Note: Convert DOS cr-lf to unix style in emacs: C-x RET f then unix 

# Source function library (just used for 'action').  If you don't have this
# it SHOULD automatically use the inline definition for "action()".
if [ -f "/etc/init.d/functions" ]; then
    . /etc/init.d/functions
else
# Run some action. Log its output. No fancy colors. First argument is the
# label for the log file.  Remaining arguments are the command to execute
# and its arguments, if any.
    action() {
        local STRING rc
        STRING=$1
        echo -n "$STRING "
        shift
        "$@" && echo -n "[OK]" || echo -n "[FAILED]"
        rc=$?
        echo
        return $rc
    }
fi

# Where the scripts live.
cd `dirname $0`

##
# Highly Recommended OS Tuning.
##

# Do not swap out applications while there is free memory.
#/sbin/sysctl -w vm.swappiness=0

# Setup the environment.
source /etc/default/bigdataHA

if [ -z "$binDir" ]; then
   echo $"$0 : environment not setup: binDir is undefined."
   exit 1;
fi
if [ -z "$pidFile" ]; then
   echo $"$0 : environment not setup: pidFile is undefined"
   exit 1;
fi
if [ -z "$BD_USER" ]; then
   echo $"$0 : environment not setup: BD_USER is undefined"
   exit 1;
fi
if [ -z "$BD_GROUP" ]; then
   echo $"$0 : environment not setup: BD_GROUP is undefined"
   exit 1;
fi

cd $FED_DIR

if [ ! -d "$DATA_DIR/lost+found" ]; then
	mount /dev/vg/lv_bigdata $DATA_DIR
	action $"`date` : `hostname` : restoring bigdata journal file: " sudo -u $BD_USER -g $BD_GROUP bin/HARestore
fi



#
# See how we were called.
#
case "$1" in
    start)
#
# Start the ServiceStarter and child services if not running.
#
	if [ -f "$pidFile" ]; then
	    read pid < "$pidFile"
	    pidno=$( ps ax | grep $pid | awk '{ print $1 }' | grep $pid )
	    if [ -z "$pidno" ]; then
# The process has died so remove the old pid file.
		echo $"`date` : `hostname` : $pid died?"
		rm -f "$pidFile"
	    fi
	fi
	if [ ! -f "$pidFile" ]; then
		action $"`date` : `hostname` : bringing up services: " sudo -u $BD_USER -g $BD_GROUP bash -c "source /etc/default/bigdataHA ; $binDir/startHAServices > /dev/null 2>&1 &"
	else
            echo $"`date` : `hostname` : running as $pid"
	fi 
        ;;
    stop)
#
# Stop the ServiceStarter and all child services.
#
	if [ -f "$pidFile" ]; then
	    read pid < "$pidFile"
	    pidno=$( ps ax | grep $pid | awk '{ print $1 }' | grep $pid )
	    if [ -z "$pidno" ]; then
# The process has died so remove the old pid file.
		echo $"`date` : `hostname` : $pid died?"
		rm -f "$pidFile"
	    else
		action $"`date` : `hostname` : bringing down services: " sudo -u $BD_USER -g $BD_GROUP kill $pid
		rm -f "$pidFile"
	    fi
	fi
        ;;
    status)
#
# Report status for the ServicesManager (up or down).
#
	if [ -f "$pidFile" ]; then
	    read pid < "$pidFile"
	    pidno=$( ps ax | grep $pid | awk '{ print $1 }' | grep $pid )
	    if [ -z "$pidno" ]; then
		echo $"`date` : `hostname` : process died? pid=$pid."
	    else
		echo $"`date` : `hostname` : running as $pid."
	    fi
	else
	    echo $"`date` : `hostname` : not running."
	fi
	;;
    restart)
	$0 stop
	$0 start
	;;
    *)
#
# Usage
#
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
esac

exit 0
